package number_61;

public class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null) {
            return head;
        }
        int length = 1;
        ListNode tail = head;
        while (tail.next != null) {
            tail = tail.next;
            length++;
        }
        k = k % length;

        int n = length - k;
        ListNode prev = head;
        while (--n > 0) {
            prev = prev.next;
        }
        tail.next = head;
        ListNode ret = prev.next;
        prev.next = null;
        return ret;

    }

    static class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
  }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode node1 = new ListNode(2);

        head.next = node1;

        new Solution().rotateRight(head, 0);
    }
}
